package com.leetcode.multithreading.anyan;

public class Jm_NotSafe {

    private long count = 0;

    public long get() {
       return  count;
    }

    public void set(long count) {
        this.count = count;
    }

    public synchronized void addCount() {
        this.count++;
    }

    public static class TestNotSafe extends Thread{
        private Jm_NotSafe jmNotSafe;

        public TestNotSafe(Jm_NotSafe jmNotSafe) {
            this.jmNotSafe = jmNotSafe;
        }

        @Override
        public void run() {
            for (int i = 0; i < 10000; i++) {
                jmNotSafe.addCount();
            }
        }
    }

    public static void main(String[] args) throws Exception {
        Jm_NotSafe jm = new Jm_NotSafe();
        TestNotSafe tns1 = new TestNotSafe(jm);
        TestNotSafe tns2 = new TestNotSafe(jm);
        tns1.start();
        tns2.start();
        Thread.sleep(3000);
        System.out.println(jm.count);//不加synchronized时，结果出现过12026
    }
}
